# -*- coding: utf-8 -*-
import scrapy

from douban.items import BoosZhipingItem


class BoosSpider(scrapy.Spider):
    name = 'boos'
    allowed_domains = ['https://www.zhipin.com/']
    query = '软件测试工程师'
    start_urls = ['https://www.zhipin.com/job_detail/?query='+query+'&scity=101200100']

    def parse(self, response):
        job_list = response.xpath("//div[@class='job-list']/ul/li")
        item = BoosZhipingItem()
        for i in job_list:
            item['Job_Salary'] = i.xpath(".//h3/a/span/text()").extract_first()
            item['work_place'] = i.xpath(".//p/text()").extract_first()
            p = i.xpath(".//div[@class='info-primary']/p").extract_first()
            p = p.split('<em class="vline"></em>')
            item['Working_years'] = p[1]
            item['Bachelor_specialist'] = p[2]
            item['Company'] = i.xpath(".//div[@class='job-primary']/div[@class='info-company']/div[@class='company-text']/h3/a/text()").extract_first()
            yield item
        next_link =response.xpath("//div[@class='page']/a[@class='next']/@href").extract()
        if next_link:
            next_link = next_link[0]
            yield scrapy.Request("https://www.zhipin.com"+next_link,callback=self.parse,dont_filter=True)

